package gui.Machine;

import gui.AddRemoveDialog;
import gui.MainFrame;
import gui.Reparation.ReparationCUDialog;
import gui.Reparation.ReparationCUDialog.DoTick;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

public class MachineCUDialog extends JDialog {

	private JButton ok, finish, addRemSp, addRemRT, create ;
	private JLabel clock, info;
	private JPanel pCenter;
	private JLabel lID, lMachineType, lName, lDescription;
	private JTextField txtID, txtName, txtDescription;
	private JComboBox cbxMachineType;

	public MachineCUDialog(javax.swing.JFrame owner, String title) {
		super(owner, title, true);
		setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		setBounds(220, 200, 300, 350);
		getContentPane().setLayout(new BorderLayout());
		setModal(true);

		pCenter = new JPanel(null);
		pCenter.setBorder(BorderFactory.createTitledBorder("Details:"));
		getContentPane().add(pCenter, BorderLayout.CENTER);

		getFields();
		getButton();
		getClock();
	}

	private void getFields() {

		int labelW = 70;
		int idY = 20, mY = 50, spY = 80, arspY = 110, rtY = 140, arrtY = 170;

		lID = new JLabel("ID:");
		lID.setBounds(20, idY, labelW, 22);
		pCenter.add(lID);

		lMachineType = new JLabel("MachineType:");
		lMachineType.setBounds(20, mY, labelW, 22);
		pCenter.add(lMachineType);

		lName = new JLabel("Name:");
		lName.setBounds(20, spY, labelW, 22);
		pCenter.add(lName);

		lDescription = new JLabel("Description:");
		lDescription.setBounds(20, rtY, labelW, 22);
		pCenter.add(lDescription);

		txtID = new JTextField();
		txtID.setBounds(30 + labelW, idY, 120, 22);
		pCenter.add(txtID);

		cbxMachineType = new JComboBox();
		cbxMachineType.setBounds(30 + labelW, mY, 120, 22);
		pCenter.add(cbxMachineType);
		
		txtName = new JTextField();
		txtName.setBounds(30 + labelW, spY, 120, 22);
		pCenter.add(txtName);
		
		txtDescription = new JTextField();
		txtDescription.setBounds(30 + labelW, rtY, 120, 22);
		pCenter.add(txtDescription);
	}

	private void getButton() {
		JPanel pSouthUp = new JPanel(new FlowLayout(FlowLayout.LEFT));
		pSouthUp.setBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED));
		info = new JLabel("info");
		pSouthUp.add(info);

		JPanel pSouthDown = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		finish = new JButton("Finish.");
		finish.setEnabled(true);
		ok = new JButton("Create");

		JPanel pSouth = new JPanel(new BorderLayout());
		pSouth.add(pSouthUp, BorderLayout.NORTH);
		pSouth.add(pSouthDown, BorderLayout.SOUTH);

		pSouthDown.add(finish);
		pSouthDown.add(ok);
		getContentPane().add(pSouth, BorderLayout.SOUTH);
	}

	private void getClock() {
		clock = new JLabel("");
		clock.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 12));
		clock.setForeground(Color.BLACK);
		getContentPane().add(clock, BorderLayout.NORTH);

		manageTime();
	}

	public void manageTime() {
		Timer timer = new Timer();
		timer.schedule(new DoTick(), 0, 1000); // update time every second

	}

	public class DoTick extends TimerTask {

		@Override
		public void run() {
			long d = new Date().getTime();
			String s2 = MainFrame.getSimpleDateFormat().format(d);
			clock.setText(s2);
		}
	}
}
